// Author: Domien Nowicki


#ifndef RAYCASTERWIDGET_H
#define RAYCASTERWIDGET_H

#include <QWidget>

#include <QResizeEvent>
#include <QGridLayout>

#include "Raycaster.h"
#include "Poly.h"
#include "Pixel.h"
#include "RendererWidget.h"
#include "GLRendererWidget.h"
#include "SoftRendererWidget.h"
#include "World.h"

class RaycasterWidget : public QWidget, public Raycaster
{
    Q_OBJECT

public:
	enum Renderer
	{
		OPENGL,
		SOFTWARE
	};

	RaycasterWidget(RaycasterWidget::Renderer rendererType, QWidget *parent = 0, Qt::WFlags flags = 0);
    virtual ~RaycasterWidget();

	void SetRenderer(RaycasterWidget::Renderer rendererType);
	void SetPixel(unsigned int x, unsigned int y, const Pixel& pixel);
	Pixel GetPixel(unsigned int x, unsigned int y) const;
	unsigned int GetHeight() const;
	unsigned int GetWidth() const;
	void DrawPolygon(const Poly& poly);
	void Update();

protected:
	void timerEvent(QTimerEvent *e);
	void paintEvent(QPaintEvent *e);
	void resizeEvent(QResizeEvent *e);
	QSize sizeHint() const;
	QSize minimumSizeHint() const;
	QSize maximumSizeHint() const;


	void keyReleaseEvent(QKeyEvent *e);
	void keyPressEvent(QKeyEvent *e);
	void mousePressEvent(QMouseEvent *e);
	void mouseReleaseEvent(QMouseEvent *e);
	void mouseMoveEvent(QMouseEvent *e);

private:
	
	enum Keys
	{
		KEY_UP,
		KEY_DOWN,
		KEY_LEFT,
		KEY_RIGHT,
		KEY_MAX
	};

	void InitKeys();
	bool KeyPressed(enum RaycasterWidget::Keys key);
	void CalculateMouseMovement(double x, double y);
	

	QGridLayout *mainLayout;
	RendererWidget *renderer;
	double lastMouseX, lastMouseY, ratioTurn, ratioMove;
	bool mousePressed;
	bool keys[KEY_MAX];

};

#endif // RAYCASTERWIDGET_H
